var express = require("express");
var fs = require("fs");
var server = express();
var mysql = require("mysql");
var url = require("url");
var expressStatic = require("express-static");
var port = 82;
var db = mysql.createConnection({
	host:"localhost",
	user:"root",
	password:"admin",
	database:"todolist"
})
db.connect();
server.get("/sigin",(request,response)=>{
	var parse = url.parse(request.url,true).query;
	var Accountnumber = parse.Accountnumber;
	var Password = parse.Password;
	if(Accountnumber && Password){
		db.query(`SELECT * FROM todolist WHERE Accountnumber="${Accountnumber}"`,(error,data)=>{
			if(data.length){
				if (data[0].Password == Password) {
					console.log("登录成功！")
					response.end("1")
				}
				else{
					console.log("登录失败！")
					response.end("error")
				}
			}
			else{
				console.log("登录失败！")
				response.end("0")
			}
		})
	}
	else{
		console.log("登录失败！")
		response.end("error")
	}
})

server.get("/register",(request,response)=>{
	var parse = url.parse(request.url,true).query;
	var Accountnumber = parse.Accountnumber;
	var Password = parse.Password;

	if(Accountnumber && Password){
		db.query(`select * from todolist where Accountnumber="${Accountnumber}"`,(error,data)=>{
			if(data.length){
				console.log(data)

				console.log("此账号已注册！")
				response.end("error");
			}
			else{
				db.query(`INSERT INTO todolist (Accountnumber,Password) VALUES ("${Accountnumber}","${Password}")`,(error,data)=>{
					if (error) {
						console.log(`注册失败，${Accountnumber}注册失败，请求接口`+ port);
						response.end("error")
					}
					else{
						console.log(`注册成功，${Accountnumber}注册成功，请求接口` + port);
						response.end("success")
					}
				})

			}
		})
	}
	else{
		console.log("账号不能为空");
		response.end("注册失败")
	}
})

setInterval(function() {
	db.query(`SELECT * FROM todolist`)
},720000)
server.listen(port);
server.use(expressStatic(__dirname + "/static"))
console.log(`runing of "${port}"` );